import Vue from 'vue'
import VueRouter from 'vue-router'

// 2.引入store，获取登录状态
import store from '../store'

// const Login = function (){
//   return import('../views/Login.vue')
// }

const Login = () => import('../views/Login.vue')
const Index = () => import('../views/Index.vue')
const Menu = () => import('../views/menu/index.vue')
const Role = () => import('../views/role/index.vue')
const User = () => import('../views/user/index.vue')
const Category = () => import('../views/category/index.vue')
const Specs = () => import('../views/specs/index.vue')
const Goods = () => import('../views/goods/index.vue')
const Member = () => import('../views/member/index.vue')
const Banners = () => import('../views/banners/index.vue')
const Seckill = () => import('../views/seck/index.vue')
const indexData = () => import('../views/indexdata/index.vue')
const Order = () => import('../views/order/order.vue')


Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: Login,
    /* 懒加载，上面不需要再引入 */
    // component:()=>import('../views/Login.vue')
  },
  {
    path: '/',
    component: Index,
    children: [
      {
        path: "/",
        component: indexData,
        meta: {
          title: '首页'
        }
      },
      {
        path: "/menu",
        component: Menu,
        meta: {
          title: '菜单列表'
        }
      },
      {
        path: "/role",
        component: Role,
        meta: {
          title: '角色管理'
        }
      },
      {
        path: "/user",
        component: User,
        meta: {
          title: '管理员管理'
        }
      },
      {
        path: "/category",
        component: Category,
        meta: {
          title: '商品分类'
        }
      },
      {
        path: "/specs",
        component: Specs,
        meta: {
          title: '商品规格'
        }
      },
      {
        path: "/goods",
        component: Goods,
        meta: {
          title: '商品管理'
        }
      },
      {
        path: "/member",
        component: Member,
        meta: {
          title: '会员管理'
        }
      },
      {
        path: "/banner",
        component: Banners,
        meta: {
          title: '活动管理'
        }
      },
      {
        path: "/seckill",
        component: Seckill,
        meta: {
          title: '限时秒杀'
        }
      },
      {
        path: "/orders",
        component: Order,
        meta: {
          title: '订单管理'
        }
      },

    ]
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 全局前置守卫
// 需求：未登录时只能查看login和index页面
// 是否登录1.在vuex中判断，2.在localStorge判断
router.beforeEach((to, from, next) => {
  // 先判断是否登录
  // console.log(store.state.userInfo.token);
  if (store.state.userInfo.token) {
    // 有的话已经登录，可以查看所有页面
    next()
  } else {
    // 未登录时只能查看login和index
    if (to.path === '/login' || to.path === '/') {
      next()
    } else {
      // 先登录
      router.push('/login')
      // 下面的方式可能会导致报错
      // next('/login')
    }
  }
})


export default router
